<template>
  <div>
    <vue-scroll v-show="visible" style="height: calc(100vh - 92px); border: 1px solid #e8e8e8" detect-resize keep-show>
      <div class="dialog__body--content">
        <div class="title">图片信息</div>
        <div class="content__item">
          <div class="item__label">大小：</div>
          <div class="item__content">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-input v-model="form.width" placeholder="长" size="mini"> </el-input>
                <div class="item__sub-des">长度</div>
              </el-col>
              <el-col :span="12">
                <el-input v-model="form.height" placeholder="宽" size="mini"> </el-input>
                <div class="item__sub-des">宽度</div>
              </el-col>
            </el-row>
          </div>
          <div class="item__label"></div>
          <el-button size="mini" style="width: 100%; margin-bottom: 5px" @click.stop="onSetOriginSize"
            >原始大小</el-button
          >

          <div class="item__label">位置</div>
          <div class="item__content">
            <el-radio-group v-model="form.align" size="mini">
              <el-radio-button
                v-for="(item, index) in options.align"
                :key="index + 1"
                :label="item.value"
                size="mini"
                >{{ item.label }}</el-radio-button
              >
            </el-radio-group>
          </div>
          <div class="item__label">标题</div>
          <div class="item__content">
            <el-input v-model="form.title" placeholder="暂无标题" type="textarea" autosize disabled> </el-input>
          </div>
          <div class="item__label">图片地址</div>
          <div class="item__content">
            <el-input v-model="form.src" type="textarea" autosize placeholder="请输入图片地址" disabled> </el-input>
          </div>
        </div>
      </div>
    </vue-scroll>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      options: {
        align: [
          {
            value: 'left',
            label: '左边'
          },
          {
            value: 'center',
            label: '中间'
          },
          {
            value: 'right',
            label: '右边'
          }
        ]
      },
      form: {
        width: '',
        height: '',
        title: '',
        src: '',
        align: ''
      }
    }
  },

  watch: {
    form: {
      handler() {
        if (!this.visible) return
        if (this.element) {
          if (this.form.align == 'center') {
            this.element.style.marginLeft = 'auto'
            this.element.style.marginRight = 'auto'
            this.element.style.float = 'none'
          } else if (this.form.align == 'left') {
            this.element.style.float = 'left'
            this.element.style.marginLeft = ''
            this.element.style.marginRight = ''
          } else if (this.form.align == 'right') {
            this.element.style.float = 'right'
            this.element.style.marginLeft = ''
            this.element.style.marginRight = ''
          }
          if (this.form.width && !isNaN(this.form.width)) {
            this.element.width = Number(this.form.width)
            this.element.style.width = `${this.form.width}px`
          }
          if (this.form.height && !isNaN(this.form.height)) {
            this.element.height = Number(this.form.height)
            this.element.style.height = `${this.form.height}px`
          }
          console.log(this.element)
        }
      },
      deep: true
    }
  },

  methods: {
    show() {
      this.visible = true
    },

    hide() {
      this.visible = false
      this.form = {
        title: '',
        src: '',
        url: ''
      }
    },

    setFormAlign(align) {
      this.form.align = align
    },

    /**
     * 设置图片原始大小
     * */
    onSetOriginSize() {
      if (!this.element) {
        this.$message.error('设置图片原始大小失败！')
        return
      }
      this.element.removeAttribute('width')
      this.element.removeAttribute('height')
      this.element.style.width = 'auto'
      this.element.style.height = 'auto'
      this.setData(this.element)
    },

    setData(e) {
      if (e) {
        this.element = e
      }
      console.log('setData', e)
      this.form = {
        ...this.form,
        title: this.element.dataset?.title || '',
        src: this.element.src,
        width: this.element.width,
        height: this.element.height,
        align: ''
      }
      if (
        this.element.style.marginLeft == 'auto' ||
        this.element.style.marginRight == 'auto' ||
        this.element.style.margin == 'auto' ||
        this.element.style.margin == '0 auto' ||
        this.element.style.margin == 'auto auto'
      ) {
        this.form.align = 'center'
      } else if (this.element.style.float == 'left') {
        this.form.align = 'left'
      } else if (this.element.style.float == 'right') {
        this.form.align = 'right'
      }
    }
  }
}
</script>

<style scoped lang="scss">
$theme-color: #47c479;
.dialog__body--content {
  min-height: 201px;
  box-sizing: border-box;
  display: flex;
  flex: 1;
  flex-direction: column;
  max-height: 650px;
  overflow: auto;
  padding: 10px 16px 20px 16px;
  .title {
    margin-bottom: 10px;
    font-size: 14px;
    font-weight: bold;
    padding-bottom: 8px;
    border-bottom: 1px solid #e8e8e8;
  }

  .content__item {
    .item__label {
      line-height: 32px;
      position: relative;
    }

    .required {
      padding-left: 8px;

      &:before {
        content: '*';
        position: absolute;
        left: 0;
        font-size: 14px;
        top: 50%;
        color: red;
        transform: translateY(-50%);
      }
    }

    ::v-deep .el-input__inner {
      &:focus {
        border-color: $theme-color;
        border-right-width: 1px !important;
        outline: 0;
        box-shadow: 0 0 0 2px rgba(37, 184, 100, 0.2);
      }
    }

    ::v-deep .el-button {
      &:focus {
        color: $theme-color;
        background: #fafafa;
        border-color: $theme-color;
      }
      &:active {
        color: $theme-color;
        background: #fafafa;
        border-color: $theme-color;
        box-shadow: 0 0 0 2px rgba(37, 184, 100, 0.2);
      }
      &:hover {
        color: $theme-color;
        background: #fafafa;
        border-color: $theme-color;
      }
    }

    .item__content {
      margin-bottom: 10px;
      .item__sub-des {
        text-align: center;
        line-height: 18px;
      }
      ::v-deep .el-radio-group {
        width: 100%;
        display: flex;
        .el-radio-button {
          flex: 1;
        }
        .is-active {
          .el-radio-button__inner {
            width: 100%;
            background-color: $theme-color;
            border-color: $theme-color;
            box-shadow: -1px 0 0 0 $theme-color;
            &:hover {
              color: #fff;
            }
          }
        }
        .el-radio-button__inner {
          width: 100%;
          &:hover {
            color: $theme-color;
          }
        }
      }
    }
  }

  .content__footer {
    text-align: right;
    margin-top: 30px;
  }
}
</style>
